﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace NovoSerDes
{
    public partial class BandwidthConChannel : UIPage
    {
        BandwidthCalculator parent;
        private int conGPIOMpbs;
        private double conI2CAMpbs;
        private double conI2CBMpbs;
        private double conI2CCMpbs;
        private double forwardGPIO = 0; //正向GPIO
        private double reverseGPIO = 0; //反向GPIO

        private double reverseI2CAPass = 0; //反向I2C(透传模式)
        private double reverseI2CANonPass = 0; //反向I2C(非透传模式)
        private double reverseSPIA = 0; //反向SPI

        private double reverseI2CBPass = 0; //反向I2C(透传模式)
        private double reverseI2CBNonPass = 0; //反向I2C(非透传模式)
        private double reverseSPIB = 0; //反向SPI

        private double reverseI2CCPass = 0; //反向I2C(透传模式)
        private double reverseI2CCNonPass = 0; //反向I2C(非透传模式)
        private double reverseSPIC = 0; //反向SPI

        private int flagGPIO = 0;
        private int flagI2CA = 0;
        private int flagI2CB = 0;
        private int flagI2CC = 0;

        public BandwidthConChannel()
        {
            InitializeComponent();
        }

        public BandwidthConChannel(BandwidthCalculator _parent)
        {
            InitializeComponent();
            parent = _parent;
        }

        public int ConGPIOMpbs
        {
            get { return conGPIOMpbs; }
        }

        public double ForwardGPIO
        {
            get { return forwardGPIO; }
        }

        public double ReverseGPIO
        {
            get { return reverseGPIO; }
        }

        public double ReverseI2CAPass
        {
            get { return reverseI2CAPass; }
        }

        public double ReverseI2CANonPass
        {
            get { return reverseI2CANonPass; }
        }

        public double ReverseSPIA
        {
            get { return reverseSPIA; }
        }

        public double ReverseI2CBPass
        {
            get { return reverseI2CBPass; }
        }

        public double ReverseI2CBNonPass
        {
            get { return reverseI2CBNonPass; }
        }

        public double ReverseSPIB
        {
            get { return reverseSPIB; }
        }

        public double ReverseI2CCPass
        {
            get { return reverseI2CCPass; }
        }

        public double ReverseI2CCNonPass
        {
            get { return reverseI2CCNonPass; }
        }

        public double ReverseSPIC
        {
            get { return reverseSPIC; }
        }

        private void CalConChannel()
        {
            conGPIOMpbs = uiIntegerUpDown_conGPIOMpbs.Value;
            conI2CAMpbs = (double)uiIntegerUpDown_conI2CAkpbs.Value / 1000;
            conI2CBMpbs = (double)uiIntegerUpDown_conI2CBkpbs.Value / 1000;
            conI2CCMpbs = (double)uiIntegerUpDown_conI2CCkpbs.Value / 1000;

            forwardGPIO = flagGPIO * conGPIOMpbs * 128 / 0.80;
            reverseGPIO = flagGPIO * conGPIOMpbs * 128 / 0.75;

            reverseI2CAPass = flagI2CA * conI2CAMpbs * 8 / 0.75;
            reverseI2CANonPass = flagI2CA * conI2CAMpbs * 2.5 / 0.75;
            reverseSPIA = flagI2CA * conI2CAMpbs * 2.5 / 0.75;

            reverseI2CBPass = flagI2CB * conI2CBMpbs * 8 / 0.75;
            reverseI2CBNonPass = flagI2CB * conI2CBMpbs * 2.5 / 0.75;
            reverseSPIB = flagI2CB * conI2CBMpbs * 2.5 / 0.75;

            reverseI2CCPass = flagI2CC * conI2CCMpbs * 8 / 0.75;
            reverseI2CCNonPass = flagI2CC * conI2CCMpbs * 2.5 / 0.75;
            reverseSPIC = flagI2CC * conI2CCMpbs * 2.5 / 0.75;

            uiTextBox_forwardGPIO.Text = forwardGPIO.ToString("F3");
            uiTextBox_reverseGPIO.Text = reverseGPIO.ToString("F3");
            uiTextBox_reverseI2CAPass.Text = reverseI2CAPass.ToString("F3");
            uiTextBox_reverseI2CANonPass.Text = reverseI2CANonPass.ToString("F3");
            uiTextBox_reverseSPIA.Text = reverseSPIA.ToString("F3");
            uiTextBox_reverseI2CBPass.Text = reverseI2CBPass.ToString("F3");
            uiTextBox_reverseI2CBNonPass.Text = reverseI2CBNonPass.ToString("F3");
            uiTextBox_reverseSPIB.Text = reverseSPIB.ToString("F3");
            uiTextBox_reverseI2CCPass.Text = reverseI2CCPass.ToString("F3");
            uiTextBox_reverseI2CCNonPass.Text = reverseI2CCNonPass.ToString("F3");
            uiTextBox_reverseSPIC.Text = reverseSPIC.ToString("F3");
            parent.CalculatorAll();
        }

        private void uiIntegerUpDown_conGPIOMpbs_ValueChanged(object sender, int value)
        {
            CalConChannel();
        }

        private void uiCheckBox_GPIOen_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_GPIOen.Checked)
            {
                flagGPIO = 1;
            }
            else
            {
                flagGPIO = 0;
            }
            CalConChannel();
        }

        private void uiCheckBox_I2CAen_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_I2CAen.Checked)
            {
                flagI2CA = 1;
            }
            else
            {
                flagI2CA = 0;
            }
            CalConChannel();
        }

        private void uiIntegerUpDown_conI2CAkpbs_ValueChanged(object sender, int value)
        {
            CalConChannel();
        }

        private void uiCheckBox_I2CBen_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_I2CBen.Checked)
            {
                flagI2CB = 1;
            }
            else
            {
                flagI2CB = 0;
            }
            CalConChannel();
        }

        private void uiIntegerUpDown_conI2CBkpbs_ValueChanged(object sender, int value)
        {
            CalConChannel();
        }

        private void uiCheckBox_I2CCen_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox_I2CCen.Checked)
            {
                flagI2CC = 1;
            }
            else
            {
                flagI2CC = 0;
            }
            CalConChannel();
        }

        private void uiIntegerUpDown_conI2CCkpbs_ValueChanged(object sender, int value)
        {
            CalConChannel();
        }
    }
}
